<!--主页图表-->
<template>
  <div ref="chart" style="width: 100%; height: 400px;"></div>
</template>

<script>
import axios from 'axios';
import * as echarts from 'echarts';

export default {
  name: 'Chart',
  data() {
    return {
      chartData: []
    };
  },
  mounted() {
    this.fetchChartData();
  },
  methods: {
    fetchChartData() {
      axios.get('/api/chart')//接口
          .then(response => {
            this.chartData = response.data;
            this.renderChart();
          })
          .catch(error => {
            console.error('Error fetching chart data:', error);
          });
    },
    renderChart() {
      const chart = echarts.init(this.$refs.chart);
      const option = {
        xAxis: {
          type: 'category',
          data: this.chartData.map(item => item.label)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.chartData.map(item => item.value),
          type: 'bar'
        }]
      };
      chart.setOption(option);
    }
  }
};
</script>

<style scoped>

</style>
